/* add user code begin Header */
/**
  **************************************************************************
  * @file     main.c
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
/* add user code end Header */

/* Includes ------------------------------------------------------------------*/
#include "at32f403a_407_wk_config.h"

/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "output.h"
#include "input.h"
#include "stdio.h"
#include "bsp.h"
#include "Lcd_Driver.h"
#include "QDTFT_demo.h"
#include "GUI.h"

/* add user code end private includes */

/* private typedef -----------------------------------------------------------*/
/* add user code begin private typedef */

/* add user code end private typedef */

/* private define ------------------------------------------------------------*/
/* add user code begin private define */

/* add user code end private define */

/* private macro -------------------------------------------------------------*/
/* add user code begin private macro */
enum SYS_STATUS
{
    SYS_STATUS_IDLE = 0,
    SYS_STATUS_RUN,
    SYS_STATUS_SET_MODE,
    SYS_STATUS_SET_PWM,
    SYS_STATUS_SET_TIME
};

enum MODE
{
    MODE_LENGHAN = 0,
    MODE_ZUHAN
};
/* add user code end private macro */

/* private variables ---------------------------------------------------------*/
/* add user code begin private variables */
uint32_t test = 0;

uint8_t mos_state=0;
uint8_t yinhu_state=0;
uint8_t yaqi_state=0;
uint8_t mianzhao_state=0;
uint32_t last_1s_time=0;

uint32_t out_pwm = 10;
uint32_t out_time = 10;
uint8_t mode = MODE_LENGHAN;

uint8_t sys_status = SYS_STATUS_IDLE;
/* add user code end private variables */

/* private function prototypes --------------------------------------------*/
/* add user code begin function prototypes */

/* add user code end function prototypes */

/* private user code ---------------------------------------------------------*/
/* add user code begin 0 */

/* add user code end 0 */

/**
  * @brief main function.
  * @param  none
  * @retval none
  */
int main(void)
{
    /* add user code begin 1 */
    uint32_t last = 0;
    uint8_t temp = 0;
    uint8_t buf[100];
    /* add user code end 1 */

    /* system clock config. */
    wk_system_clock_config();

    /* config periph clock. */
    wk_periph_clock_config();

    /* init debug function. */
    wk_debug_config();

    /* nvic config. */
    wk_nvic_config();

    /* init usart1 function. */
    wk_usart1_init();

    /* init spi1 function. */
    wk_spi1_init();

    /* init i2c1 function. */
    wk_i2c1_init();

    /* init exint function. */
    wk_exint_config();

    /* init gpio function. */
    wk_gpio_config();
    
    systick_init();
    
    wk_tmr3_init();
    
    wk_tmr4_init();
    
    Lcd_Init();
    
    Lcd_Clear(BLACK);

    gui_draw_string(100,LINE0, GREEN, BLACK, (uint8_t *)"TIG Controler");
    
    /*status*/
    gui_draw_string(20,LINE2, GREEN, BLACK, (uint8_t *)"STATUS:");
    gui_draw_string(92,LINE2, RED, BLACK, (uint8_t *)"IDLE    ");
    
    /*mode*/
    gui_draw_string(20,LINE3, GREEN, BLACK, (uint8_t *)"MODE  :");
    gui_draw_string(92,LINE3, GREEN, BLACK, (uint8_t *)"Leng Han");
    /*pwm*/
    gui_draw_string(20,LINE4, GREEN, BLACK, (uint8_t *)"PWM   :");
    sprintf((char *)buf,"%d  ", out_pwm);
    gui_draw_string(92,LINE4, GREEN, BLACK, buf);
    /*time*/
    gui_draw_string(20,LINE5, GREEN, BLACK, (uint8_t *)"TIME  :");
    sprintf((char *)buf,"%d  ", out_time);
    gui_draw_string(92,LINE5, GREEN, BLACK, buf);
    
    /* add user code begin 2 */
    printf("Leng Han Ji Controler\r\n");
    
    /* add user code end 2 */

    while(1)
    {
    /* add user code begin 3 */
        
        switch(sys_status)
        {
            case SYS_STATUS_IDLE:
                if(enc_key)
                {
                    enc_key = 0;
                    gui_draw_string(92,LINE2, GREEN, BLACK, (uint8_t *)"RUN     ");
                    sys_status = SYS_STATUS_RUN;
                }
                break;
            case SYS_STATUS_RUN:
                if(enc_key)
                {
                    enc_key = 0;
                    gui_draw_string(92,LINE2, YELLOW, BLACK, (uint8_t *)"SET MODE");
                    sys_status = SYS_STATUS_SET_MODE;
                }
                
                if(input_changed[0])
                {
                    input_changed[0] = 0;
                    
                    if(input_status[0])
                    {
//                        out_diancifa(1);
//                        timer_delay(1000);       
                        out_mos(out_pwm);
                        out_yinhu(1);
                        timer_delay(out_time);       
                        out_yinhu(0);
                        out_mos(0);
//                        timer_delay(1000);   
//                        out_diancifa(0);
                    }
                }
                break;
            case SYS_STATUS_SET_MODE:
                if(enc_key)
                {
                    enc_key = 0;
                    gui_draw_string(92,LINE2, YELLOW, BLACK, (uint8_t *)"SET PWM ");
                    sys_status = SYS_STATUS_SET_PWM;
                }
                if(enc_up || enc_dn)
                {
                    enc_up = 0;
                    enc_dn = 0;
                    if(mode == MODE_LENGHAN)
                    {
                        mode = MODE_ZUHAN;
                        gui_draw_string(92,LINE3, GREEN, BLACK, (uint8_t *)"Zu Han  ");
                    }
                    else
                    {
                        mode = MODE_LENGHAN;
                        gui_draw_string(92,LINE3, GREEN, BLACK, (uint8_t *)"Leng Han  ");
                    }
                }
                break;
            case SYS_STATUS_SET_PWM:
                if(enc_key)
                {
                    enc_key = 0;
                    gui_draw_string(92,LINE2, YELLOW, BLACK, (uint8_t *)"SET TIME");
                    sys_status = SYS_STATUS_SET_TIME;
                }
                if(enc_up)
                {
                    enc_up = 0;
                    if(out_pwm<100)
                    {
                        out_pwm++;
                        sprintf((char *)buf,"%d  ", out_pwm);
                        gui_draw_string(92,LINE4, GREEN, BLACK, buf);
                    }
                }
                if(enc_dn)
                {
                    enc_dn = 0;
                    if(out_pwm > 0)
                    {
                        out_pwm--;
                        sprintf((char *)buf,"%d  ", out_pwm);
                        gui_draw_string(92,LINE4, GREEN, BLACK, buf);
                    }
                }
                break;
            case SYS_STATUS_SET_TIME:
                if(enc_key)
                {
                    enc_key = 0;
                    gui_draw_string(92,LINE2, RED, BLACK, (uint8_t *)"IDLE    ");
                    sys_status = SYS_STATUS_IDLE;
                }
                if(enc_up)
                {
                    enc_up = 0;
                    if(out_time<100)
                    {
                        out_time++;
                        sprintf((char *)buf,"%d  ", out_time);
                        gui_draw_string(92,LINE5, GREEN, BLACK, buf);
                    }
                }
                if(enc_dn)
                {
                    enc_dn = 0;
                    if(out_time > 0)
                    {
                        out_time--;
                        sprintf((char *)buf,"%d  ", out_time);
                        gui_draw_string(92,LINE5, GREEN, BLACK, buf);
                    }
                }
                break;
        }       
               
        if(millis() - last > 100)
        {
            last = millis();
            test++;
            
            if(temp)
            {
                temp = 0;
            }
            else
            {
                temp = 1;
            }            
        }
        
        if(millis() - last_1s_time >=1000)
        {
            sprintf((char *)buf, "%d:%02d", run_time/60, run_time%60);
            
            gui_draw_string(20,LINE14, WHITE, BLACK, buf);
        }        
        
    /* add user code end 3 */
    }
}



